<template>
  <div>
    <!-- 按钮 -->
    <el-button
      type="primary"
      icon="el-icon-plus"
      style="margin: 10px 0px"
      @click="showDialog"
      >添加</el-button
    >
    <!-- 表格组件
    data：表格组件将来需要展示的数据-----数组类型
    border：给表格添加边框
    column：属性
    label：列标题
    width：对应的宽度
    align：标题的对齐方式 -->
    <el-table style="width = 100%" :data="list" border>
      <el-table-column type="index" label="序号" width="80px" align="center">
      </el-table-column>
      <el-table-column prop="title" label="品牌名称" width="width">
      </el-table-column>
      <el-table-column prop="imageUrl" label="品牌LOGO" width="width">
        <template slot-scope="{ row, $index }">
          <img :src="row.imageUrl" style="width: 100px; height: 100px" />
        </template>
      </el-table-column>
      <el-table-column prop="prop" label="操作" width="width">
        <template slot-scope="{ row, $index }">
          <el-button
            type="warning"
            icon="el-icon-edit"
            size="mini"
            @click="updateTradMark(row)"
            >修改</el-button
          >
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteTradMark(row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器
    分页器四个必要数据：当前第几页，数据总条数，每页显示多少条数据，连续页码数
    :total 总共多少条数据
    :page-size="4" 每页显示4条数据
    layout  分页器布局
    :page-count="9" 按钮的数量 如果是9出去首页和末页的页码，连续的页码数是7
    :current-page="6"  当前的页数 -->
    <el-pagination
      style="margin-top: 20px; text-align: center"
      :total="total"
      :current-page="page"
      :page-size="limit"
      :page-count="7"
      :page-sizes="[3, 5, 10]"
      layout="prev,pager,next,jumper,->,sizes,total"
      @current-change="getPageList"
      @size-change="handleSizeChange"
    >
    </el-pagination>

    <el-dialog
      :visible.sync="dialogFormVisible"
      :title="tmForm.id ? '修改品牌' : '添加品牌'"
      center
    >
      <!-- model属性把表单的信息收集到tmForm变量中 -->
      <el-form style="width: 80%" :model="tmForm" :rules="rules" ref="ruleForm">
        <el-form-item label="品牌名称" :label-width="'100px'" prop="tmName">
          <el-input v-model="tmForm.tmName" />
        </el-form-item>
        <el-form-item label="品牌LOGO" :label-width="'100px'" prop="logoUrl">
          <el-upload
            class="avatar-uploader"
            action="dev-api/admin/product/fileUpload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="tmForm.logoUrl" :src="tmForm.logoUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="addOrUpdateTradeMark('ruleForm')">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "tradeMark",
  data() {
    return {
      page: 1, //当前页码
      limit: 3, //当前页数展示的条数
      list: [], //列表展示的数据
      total: 0, //总共数据条数
      dialogFormVisible: false, //显示对话框
      tmForm: {
        //收集品牌的信息
        logoUrl: "",
        tmName: "",
      },
      rules: {
        tmName: [
          { required: true, message: "请输入品牌名称", trigger: "blur" },
          { min: 2, max: 10, message: "长度在 2 到 10 个字符", trigger: "change" },
        ],
        logoUrl: [
          {required: true, message: "请选择品牌图片", trigger: "change"}
        ]
      },
    };
  },
  mounted() {
    this.getPageList();
  },
  methods: {
    async getPageList(pager = 1) {
      this.page = pager;
      const { page, limit } = this;
      let result = await this.$API.trademark.reqTradeMarkList(page, limit);
      if (result.code == 200) {
        this.total = result.data.total;
        this.list = result.data.records;
      }
      console.log(result.data.records);
    },
    handleSizeChange(limit) {
      this.limit = limit;
      this.getPageList(this.page);
    },
    showDialog() {
      //添加按钮方法
      this.dialogFormVisible = true;
      //清楚数据
      this.tmForm = { logoUrl: "", tmName: "" };
    },
    updateTradMark(row) {
      //修改按钮方法
      this.dialogFormVisible = true;
      this.$set(this.tmForm, "logoUrl", row.imageUrl);
      this.$set(this.tmForm, "tmName", row.title);
      this.$set(this.tmForm, "id", row.id);
      // this.tmForm={
      //   logoUrl:row.imageUrl,
      //   tmName:row.title,
      //   id:row.id,
      // }
      // this.tmForm= {...row};
    },
    handleAvatarSuccess(res, file) {
      //上传成功后的回调
      this.tmForm.logoUrl = res.data;
    },
    beforeAvatarUpload(file) {
      //上传之前的回调
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    addOrUpdateTradeMark(ruleForm) {
      //添加品牌里的确定按钮
      this.$refs[ruleForm].validate(async (valid) => {
        if (valid) {
          this.dialogFormVisible = false;
          let result = await this.$API.trademark.reqAddOrUpdateTradeMark(
            this.tmForm
          );
          console.log(result);
          if (result.code == 200) {
            this.$message({
              type: "success",
              message: this.tmForm.id ? "修改品牌成功" : "添加品牌成功",
            });
            this.getPageList(this.tmForm.id ? this.page : 1);
          }
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    deleteTradMark(row){
      console.log(row);
      this.$confirm(`你确定删除${row.title}?`,"提示",{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() =>{
        console.log(1);
        let result= await this.$API.trademark.reqDeleteTradMark(row.id);
        if(result.code==200){
          this.$message({
          type:"success",
          message:"删除成功！"
          })
          this.getPageList()
        }
      }).catch(()=>{ 
        console.log(2);
        this.$message({
          type:"info",
          message:"已取消删除"
        })
      })
    }
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>